package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.service.MarketLogService;
import com.cskaoyan.market.service.impl.MarketLogServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * ClassName: AdminLogController
 * Package: com.cskaoyan.market.controller
 * Date: 2023/5/8 - 14:11
 * Description:
 */
@RestController
@RequestMapping("admin/log")
public class AdminLogController {
    @Autowired
    MarketLogService marketLogService;

    @GetMapping("list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userName = req.getParameter("name");
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        if (StringUtils.isEmpty(sort)) {
            sort = "id";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        if (!("asc".equals(order) || "desc".equals(order))) {
            Object o = ResponseUtil.badArgumentValue();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        List<MarketLog> marketLogs = marketLogService.list(page, limit, sort, order,userName);
        if(marketLogs==null){
            Object serious = ResponseUtil.fail();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(serious));
            return;
        }
        Object okList = ResponseUtil.okList(marketLogs);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(okList));
    }
}
